package com.jamu.mbok;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class HomeScreen extends Activity{
	Button btnPlay, btnOption, btnCredit, btnExit;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.homescreen);
        
        this.castItem();
        
        this.addAction();
        
    }
    
    public void castItem() {
    	btnPlay = (Button)findViewById(R.id.btnPlay);
    	btnOption = (Button)findViewById(R.id.btnOption);
    	btnCredit = (Button)findViewById(R.id.btnCredit);
    	btnExit = (Button)findViewById(R.id.btnExit);
	}
    
    public void addAction(){
    	btnPlay.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					btnPlay.setBackgroundResource(R.drawable.btnplayclicked);
				}else if(event.getAction()==MotionEvent.ACTION_UP){
					btnPlay.setBackgroundResource(R.drawable.btnplay);
					modeAct();
				}
				return false;
			}
		});
    	btnOption.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					btnOption.setBackgroundResource(R.drawable.btnoptionclicked);
				}else if(event.getAction()==MotionEvent.ACTION_UP){
					btnOption.setBackgroundResource(R.drawable.btnoption);
//					munculkan OptionScreen
					optionAct();
				}
				return false;
			}
		});
    	
    	btnCredit.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if(event.getAction() == MotionEvent.ACTION_DOWN){
//					btnCredit.setBackgroundResource(R.drawable.btncreditclicked);
				}else if(event.getAction()==MotionEvent.ACTION_UP){
//					btnCredit.setBackgroundResource(R.drawable.btncredit);
//					munculkan CreditScreen
					creditAct();
				}
				return false;
			}
		});

    	btnExit.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if(event.getAction() == MotionEvent.ACTION_DOWN){
//					btnExit.setBackgroundResource(R.drawable.btnexitclicked);
				}else if(event.getAction()==MotionEvent.ACTION_UP){
//					btnExit.setBackgroundResource(R.drawable.btnexit);	
//					munculkan dialogbox exit confirmation
					exit();
				}
				return false;
			}
		});

    }

    public void optionAct() {
		// TODO Auto-generated method stub
		this.startActivity(new Intent("android.intent.action.OPTIONSCREEN"));
		overridePendingTransition(R.anim.fade, R.anim.hold);
		this.finish();
	}

	public void exit() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    builder.setMessage("Are you sure you want to exit?")
	           .setTitle("Exit")
	    		.setCancelable(false)
	           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	               public void onClick(DialogInterface dialog, int id) {
	                    finish();
	               }
	           })
	           .setNegativeButton("No", new DialogInterface.OnClickListener() {
	               public void onClick(DialogInterface dialog, int id) {
	                    dialog.cancel();
	               }
	           });
	    AlertDialog alert = builder.create();
	    alert.show();
	}
    
    public void creditAct(){
		this.startActivity(new Intent("android.intent.action.CREDITSCREEN"));
		overridePendingTransition(R.anim.fade, R.anim.hold);
		this.finish();
    }
    
    public void modeAct(){
    	this.startActivity(new Intent("android.intent.action.MODESELECT"));
		overridePendingTransition(R.anim.fade, R.anim.hold);
		this.finish();
    }
}
